/**
 * 首页配置
 * <p>Title: ServiceZM9Q.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-12-07下午15:31:29  高强
 *</pre>
 */
package com.baosight.sczx.zm.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.domain.ZM9Q;



/**首页模块管理--模块信息
 * <p>Title: ServiceZM9Q.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *           2012-12-07下午15:31:29  高强
 *</pre>
 */

public class ServiceZM9Q extends ServiceEPBase {
	String fdDepartmentId ="";
	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9Q</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2012-12-07下午15:31:29   高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9Q</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-12-07下午15:31:29   高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		fdDepartmentId = inInfo.getString("fdDepartmentId");//树节点ID
		if(fdDepartmentId != null){
			StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);//添加查询条件
		    }
		EiInfo outInfo = super.query(inInfo, "ZM9Q.query",new ZM9Q());
		return outInfo;
	}
	
	/**列表查询
	 * <p>Function: queryModule</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9Q</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-12-07下午15:31:29   高强
	 * </p>
	 *
	*/
	public EiInfo queryModule(EiInfo inInfo)
	{
		EiInfo outInfo = new EiInfo();
		outInfo.addBlock("moduleResult").addBlockMeta((new ZM9Q()).eiMetadata);
		Map map = new HashMap();
		map.put("fdDepartmentId", fdDepartmentId);
		outInfo.getBlock("moduleResult").addRows(dao.query("ZM9Q.queryModule", map));
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9Q</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2012-12-07下午15:31:29   高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiBlock resultBlock = inInfo.getBlock("moduleResult");
		String fdDepartmentId = inInfo.getString("fdDepartmentId");//主表ID
		for(int i = 0; i < resultBlock.getRowCount(); i++)
		{
			String fdModuleName = inInfo.getCellStr("moduleResult", i, "fdModuleName");
			resultBlock.setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_INDEX_MODULE"));
			resultBlock.setCell(i, "fdDepartmentId", fdDepartmentId);//部门ID
			resultBlock.setCell(i, "fdModuleName", fdModuleName);
			resultBlock.setCell(i, "fdModuleId", inInfo.getCellStr("moduleResult", i, "fdModuleId"));
			resultBlock.setCell(i, "fdIshyperlink", inInfo.getCellStr("moduleResult", i, "fdIshyperlink"));
		}
		
		if(resultBlock.getRowCount() > 0) {
			inInfo = super.insert(inInfo, "ZM9Q.insert", "moduleResult");
		}
		return inInfo;
	}
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9Q</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2012-12-07下午15:31:29   高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		if(!canDelete(inInfo))
		{
			return inInfo;
		}
		super.delete(inInfo, "ZM9Q.delete");
		return this.query(inInfo);
	}
	
	/**
	 * 检查选择删除的模块中是否有被使用的
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *      2012-12-07下午15:31:29   高强
	 * </p>
	 *
	 */
	public  boolean  canDelete(EiInfo inInfo){
		for(int i=0;i<inInfo.getBlock(EiConstant.resultBlock).getRowCount();i++)
		{
			Map<String, String> _args = new HashMap<String, String>();
			_args.put("fdKeyId", inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdKeyId"));
			_args.put("fdDepartmentId", fdDepartmentId);
			List<Map> result = dao.query("ZM9Q.canDelete", _args);
			if(result != null && result.size() > 0) {
			    String msg = "模块" +result.get(0).get("fdModuleName")+ "已经被配置到首页中，不能删除！";
				inInfo.setMsg(msg);
				inInfo.setStatus(-1);
				return false;
			}
		}
		return true;
	}
}
